const db = wx.cloud.database()
const _ = db.command
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		statusTrue: "已同意",
		statusFalse: "同意",
		teams: [],
		statusList: [],
		indexList: []
	},
	status(e) {
		// console.log(e.currentTarget.dataset.index)
		let index = e.currentTarget.dataset.index
		let id = this.data.teams[index]._id
		let status = "statusList[" + index + "]"
		if(this.data.statusList[index] == 1){
			return
		}
		wx.cloud.callFunction({
			name:"agreeInvite",
			data:{
				id:id
			}
		}).then(res => {
			console.log(res)
			this.setData({
				[status]: 1
			})
			wx.showToast({
				title: '操作成功',
				icon: "none"
			})
		})
	},
	detail(e) {
		console.log(e.currentTarget.dataset)
		wx.navigateTo({
			url: '/pages/teamDetail/teamDetail?teamId=' + e.currentTarget.dataset.teamid,
		})
	},
	showMenu(e) {
		// console.log(e.currentTarget.dataset.index)
		let indexList = []
		for (let i in this.data.indexList) {
			indexList.push(0)
		}
		indexList[e.currentTarget.dataset.index] = 1
		this.setData({
			indexList: indexList
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		wx.cloud.callFunction({
			name: "getopenid"
		}).then(eres => {
			console.log(eres.result.openid)
			let openid = eres.result.openid
			db.collection('team').where({ beinvited: openid }).get().then(res => {
				console.log(res)
				let statusList = []
				let indexList = []
				for (let index in res.data) {
						if (res.data[index].teamPeople.includes(openid)) {
						indexList.push(0)
						statusList.push(1)
						continue
					}
					indexList.push(0)
					statusList.push(0)
				}
				this.setData({
					indexList: indexList,
					statusList: statusList,
					teams: res.data,
					openid: openid
				})
			})
		})

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})